package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.CommentDao;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class CommentDiscus extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int cid = TextUtil.String2int(request.getParameter("cid"), -1);
		String content = request.getParameter("content");
		int touid = TextUtil.String2int(request.getParameter("uid"), -1);
		
		//检测参数
		if (TextUtil.isEmpty(accesstoken)) {
			O.printFail(response, 1, "请检查accesstoken参数!");
			return;
		}
		
		if (cid < 0) {
			O.printFail(response, 1, "请检查cid参数!");
			return;
		}
		
		if (TextUtil.isEmpty(content)) {
			O.printFail(response, 1, "请检查content参数!");
			return;
		}
		
		// 检测连接、用户
		int uid = VeriFicUtil.getVFID(accesstoken);
		if (uid == Integer.MIN_VALUE || uid == Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		Connection conn = DBUtil.getConnection();
		if (conn == null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}

		// 开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		CommentDao cdao = dao.getCommentDao();
		try {
			touid = cdao.getCommentInfo(touid).getUserid();
		} catch (Exception e) {
		}
		
		if (cdao.sendReComment(cid, uid, touid, content)) {
			O.printSuccess(response, "评论成功!");
		} else {
			O.printFail(response, 1, "评论失败!");
		}
		
		
	}

}
